//
//  CMPasterCenterView.m
//  CropImg
//
//  Created by lipeng on 16/3/7.
//  Copyright © 2016年 lipeng. All rights reserved.
//

#import "CMPasterCenterView.h"
#import "CMPasterView.h"
#import "UIImage+AddFunction.h"

#define APPFRAME    [UIScreen mainScreen].bounds

@interface CMPasterCenterView()<CMPasterDelegate>
{
    CGPoint startPoint;
    CGPoint touchPoint;
    NSMutableArray *m_listPaster;
}

@property (nonatomic, strong) UIButton      *bgButton;
@property (nonatomic, strong) UIImageView   *imgView;
@property (nonatomic, strong) CMPasterView  *pasterCurrent;
@property (nonatomic, assign) int           newPasterID;


@end

@implementation CMPasterCenterView

#pragma mark - initial
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        m_listPaster = [[NSMutableArray alloc] initWithCapacity:1];
        [self imgView];
        [self bgButton];
        self.clipsToBounds = YES;
    }
    
    return self;
}

#pragma mark - public
-(void)addPasterWithImg:(UIImage *)imgP
{
    [self clearAllOnFirst];
    
    self.pasterCurrent = [[CMPasterView alloc] initWithBgView:self
                                                     pasterID:self.newPasterID
                                                          img:imgP];
    _pasterCurrent.delegate = self;
    [m_listPaster addObject:_pasterCurrent];
}

-(UIImage *)doneEdit
{
    [self clearAllOnFirst];
    
    CGFloat org_width = self.originImage.size.width;
    CGFloat org_height = self.originImage.size.height;
    CGFloat rateOfScreen = org_width / org_height;
    CGFloat inScreenH = self.frame.size.width / rateOfScreen;
    
    CGRect rect = CGRectZero;
    rect.size = CGSizeMake(APPFRAME.size.width, inScreenH);
    rect.origin = CGPointMake(0, (self.frame.size.height - inScreenH)/2);
    
    UIImage *imgTemp = [UIImage getImageFromView:self];
    
    UIImage *imgCut = imgTemp;//[UIImage squareImageFromImage:imgTemp scaledToSize:rect.size.width];
    
    return imgCut;
}


- (void)backgroundClicked:(UIButton *)btBg
{
    NSLog(@"back clicked");
    
    [self clearAllOnFirst];
}

-(void)clearAllOnFirst
{
    _pasterCurrent.isOnFirst = NO;
    
    [m_listPaster enumerateObjectsUsingBlock:^(CMPasterView *pasterV, NSUInteger idx, BOOL * _Nonnull stop) {
        pasterV.isOnFirst = NO;
    }];
}


#pragma mark - Properties(属性函数)
-(void)setOriginImage:(UIImage *)originImage
{
    _originImage = originImage;
    self.imgView.image = originImage;
}

-(int)newPasterID
{
    _newPasterID++;
    return _newPasterID;
}

-(void)setPasterCurrent:(CMPasterView *)pasterCurrent
{
    _pasterCurrent = pasterCurrent;
    [self bringSubviewToFront:pasterCurrent];
}

-(UIButton *)bgButton
{
    if (!_bgButton) {
        _bgButton = [[UIButton alloc] initWithFrame:self.frame];
        _bgButton.tintColor = nil;
        _bgButton.backgroundColor = nil;
        [_bgButton addTarget:self
                      action:@selector(backgroundClicked:)
            forControlEvents:UIControlEventTouchUpInside];
        if (![_bgButton superview])
        {
            [self addSubview:_bgButton];
        }
    }
    
    return _bgButton ;
}

- (UIImageView *)imgView
{
    if (!_imgView)
    {
        CGRect rect = CGRectZero;
        rect.size.width = self.frame.size.width;
        rect.size.height = self.frame.size.width;
        rect.origin.y = ( self.frame.size.height - self.frame.size.width ) / 2.0;
        _imgView = [[UIImageView alloc] initWithFrame:rect];
        
        _imgView.contentMode = UIViewContentModeScaleAspectFit;
        
        if (![_imgView superview])
        {
            [self addSubview:_imgView] ;
        }
    }
    
    return _imgView ;
}

#pragma mark - PasterViewDelegate
- (void)makePasterBecomeFirst:(int)pasterID
{
    [m_listPaster enumerateObjectsUsingBlock:^(CMPasterView *pasterV, NSUInteger idx, BOOL * _Nonnull stop) {
        
        pasterV.isOnFirst = NO ;
        
        if (pasterV.pasterID == pasterID)
        {
            self.pasterCurrent = pasterV ;
            pasterV.isOnFirst = YES ;
        }
        
    }] ;
}

- (void)removePaster:(int)pasterID
{
    [m_listPaster enumerateObjectsUsingBlock:^(CMPasterView *pasterV, NSUInteger idx, BOOL * _Nonnull stop) {
        if (pasterV.pasterID == pasterID)
        {
            [m_listPaster removeObjectAtIndex:idx] ;
            *stop = YES ;
        }
    }] ;
}

@end
